<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 The Unix Module
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora164.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora166.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> The <TT>Unix</TT> Module</H2>
<A NAME="mod-Unix"></A>
<A NAME="@fonctions400"></A>This module contains the interfaces to the most important
Unix library functions. Much of this module
has been ported to Windows and can be used there.
Whenever necessary we will indicate the restrictions in the use
of the presented functions. A table resuming the restrictions
is given by figure <A HREF="book-ora165.html#fig-portable">18.1</A>.<BR>
<BR>
The <TT>Unix</TT> library belongs to the Objective CAML non-standard
libraries which have to be bound by the <TT>-custom</TT> compiler
command (see chapter <A HREF="index.html#chap-Compilation">7</A>, page
<A HREF="index.html#chap-Compilation">??</A>). Depending on the desired form
of the program, one of the following commands is used under
Unix to produce bytecode, native code or an interaction loop:
<PRE>
$ ocamlc -custom unix.cma  fichiers.ml -cclib -lunix
$ ocamlopt unix.cma fichiers.ml -cclib -lunix
$ ocamlmktop -custom -o unixtop unix.cma -cclib -lunix 
</PRE>The purpose of constructing an interaction loop (of which the name will be
<CODE>unixtop</CODE>) is to support an incremental development style.
Each function can be compiled quickly from its type declaration.
It is also possible to execute functional tests.<BR>
<BR>
Depending on the version of Unix in use, the system library may not be located
at the default place. If necessary the access path of the libraries
may be indicated with the option <CODE>-ccopt</CODE> (see chapter
<A HREF="index.html#chap-Compilation">7</A>).<BR>
<BR>
Under Windows the commands to compile become:
<PRE>
$ ocamlc -custom unix.cma  fichiers.ml %CAMLLIB%\libunix.lib wsock32.lib
$ ocamlopt unix.cma fichiers.ml %CAMLLIB%\libunix.lib wsock32.lib
$ ocamlmktop -custom -o unixtop.exe unix.cma %CAMLLIB%\libunix.lib wsock32.lib
</PRE>
The name of the obtained interaction loop is <CODE>unixtop.exe</CODE>.<BR>
<BR>
<A NAME="toc242"></A>
<H3> Error Handling</H3>
<A NAME="@fonctions401"></A>
<A NAME="@fonctions402"></A>
<A NAME="@fonctions403"></A>
Errors produced by system calls throw <TT>Unix_error</TT> exceptions,
which can be handled by the Objective CAML program. Such errors contain
three arguments: a value of type <TT>Unix.error</TT> which can be
transformed into a character string by the function <TT>error_message</TT>,
a string containing the name of the function producing the error
and optionally, a string containing the argument of the function
when the argument is of type <I>string</I>.<BR>
<BR>
It is possible to define a generic calling function
with error treatment:


<PRE><BR># <B>let</B><CODE> </CODE>wrap_unix<CODE> </CODE>funct<CODE> </CODE>arg<CODE> </CODE><CODE>=</CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>try</B><CODE> </CODE><TT>(</TT>funct<CODE> </CODE>arg<TT>)</TT><CODE> </CODE><B>with</B><CODE> </CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>Unix<CODE>.</CODE>Unix_error<CODE> </CODE><TT>(</TT>e<CODE>,</CODE>fm<CODE>,</CODE>argm<TT>)</TT><CODE> </CODE>-&gt;<CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>Printf.printf<CODE> </CODE><CODE>"%s %s %s"</CODE><CODE> </CODE><TT>(</TT>Unix.error_message<CODE> </CODE>e<TT>)</TT><CODE> </CODE>fm<CODE> </CODE>argm<CODE> </CODE>;;<BR><CODE>val wrap_unix : ('a -&gt; unit) -&gt; 'a -&gt; unit = &lt;fun&gt;</CODE><BR>

</PRE>

The function <TT>wrap_unix</TT> takes a function and its argument,
and applies one to the other. If a Unix error occurs, an
explaining message is printed. An equivalent function is defined
in the <TT>Unix</TT> module:<BR>
<BR>
<A NAME="@fonctions404"></A>


<PRE><BR># Unix.handle_unix_error<CODE> </CODE>;;<BR><CODE>- : ('a -&gt; 'b) -&gt; 'a -&gt; 'b = &lt;fun&gt;</CODE><BR>

</PRE>
<BR>
<BR>
<A NAME="toc243"></A>
<H3> Portability of System Calls</H3>Figure <A HREF="book-ora165.html#fig-portable">18.1</A> indicates which of the communication and process
handling functions presented in this chapter are accessible under
Windows. The main shortcoming is the lack of the two functions
<TT>fork</TT> and <TT>kill</TT> to create new processes
and to send signals.<BR>
<BR>
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD  VALIGN=top ALIGN=left NOWRAP>Fonction</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>Unix</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>Windows</TD>
<TD  VALIGN=top ALIGN=left>Comment</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>openfile</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>close</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>dup</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>dup2</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>read</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>write</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>lseek</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>execv</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>execve</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>execvp</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>execvpe</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>fork</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>use <TT>create_process</TT></TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>getpid</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>sleep</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>wait</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>waitpid</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>only for a given</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>number of processes</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>create_process</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>create_process_env</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>kill</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>pipe</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>mkfifo</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>open_process</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>use the interpretation of</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left><TT>/bin/sh</TT> commands</TD>
</TR>
<TR><TD  VALIGN=top ALIGN=left NOWRAP><TT>close_process</TT></TD>
<TD  VALIGN=top ALIGN=center NOWRAP>�</TD>
<TD  VALIGN=top ALIGN=center NOWRAP>&nbsp;</TD>
<TD  VALIGN=top ALIGN=left>&nbsp;</TD>
</TR></TABLE>
<BR>
<DIV ALIGN=center>Figure 18.1: Portability of the module <TT>Unix</TT> functions
used in this chapter.</DIV><BR>

<A NAME="fig-portable"></A>
</DIV>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
Furthermore, the function <TT>wait</TT> waiting for the end of a child process
is not implemented, because <TT>fork</TT> is not.<BR>
<BR>
<HR>
<A HREF="book-ora164.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora166.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
